package ru.spb.nicetu.schoolproject.gui;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import ru.spb.nicetu.schoolproject.TblModelForLessons;
import ru.spb.nicetu.schoolproject.data.DayOfWeek;
import ru.spb.nicetu.schoolproject.data.HibernateUtil;
import ru.spb.nicetu.schoolproject.data.Lesson;
import ru.spb.nicetu.schoolproject.data.SchoolClass;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * Created by dchernyshov on 25.04.15.
 */
public class LessonReports extends LessonCard {
    private Session session;

    private List<Lesson> lessonList;

    private Box subjectReportBox;
    private Box timetableBox;

    public LessonReports() {
        setFrameSize();
    }

    public Box createReportBox(String label, ActionListener actionListener) {
        Box box = Box.createHorizontalBox();
        JButton button = new JButton("Получить ");
        button.addActionListener(actionListener);
        box.add(button);
        box.add(Box.createHorizontalStrut(10));
        box.add(new JLabel(label));
        return box;
    }

    @Override
    public Box createVerticalBox() {
        createBoxes();
        addBoxesToVerticalBox();

        subjectReportBox = createReportBox("предмет, который будет у " +
                "заданного класса в заданный день в заданый урок",
                new BtnSubjectReportListener());
        vbox.add(Box.createVerticalStrut(10));
        vbox.add(subjectReportBox);
        timetableBox = createReportBox("расписание на заданный день для класса",
                new BtnTimeTableReportListener());
        vbox.add(Box.createVerticalStrut(10));
        vbox.add(timetableBox);
        return vbox;
    }

    public void setFrameSize() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setSize(screenWidth / 2, screenHeight / 2);
        setLocationByPlatform(true);
    }

    public boolean isLessonNumSelected() {
        if (lessonNumField.getText().isEmpty()) {
            return false;
        }
        else return true;
    }

    public class BtnSubjectReportListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            if (!isLessonNumSelected()) {
                JOptionPane.showMessageDialog(null, "Введите номер урока");
                return;
            }
            subjectReport();
            if (lessonList.size() == 0) {
                JOptionPane.showMessageDialog(null, "Нет данных");
                return;
            }
            Lesson lesson = lessonList.get(0);
            JOptionPane.showMessageDialog(null, "" + lesson.getSubject().getName().trim());
        }
    }

    public class BtnTimeTableReportListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            classTimeTableReport();
            if (lessonList.size() == 0) {
                JOptionPane.showMessageDialog(null, "Нет данных");
                return;
            }
            JDialog dialog = new ReportsDialog<TblModelForLessons>(new TblModelForLessons(lessonList), true);
            dialog.setTitle("Расписание класса");
            dialog.setVisible(true);
        }
    }

    public void subjectReport() {
        session = HibernateUtil.getInstance().getSession();
        try {
            session.beginTransaction();
            org.hibernate.Query query = session.getNamedQuery("Lesson.byClassDayNum");
            SchoolClass schoolClass = (SchoolClass) classComboBox.getSelectedItem();
            query.setInteger(0, schoolClass.getClassId());
            DayOfWeek dayOfWeek = (DayOfWeek) dayComboBox.getSelectedItem();
            query.setInteger(1, dayOfWeek.getDayId());
            Number lesson = (Number) lessonNumField.getValue();
            object.setLessonNum(lesson.intValue());
            query.setInteger(2, lesson.intValue());
            lessonList = (List<Lesson>) query.list();
            session.getTransaction().commit();
        }
        catch (HibernateException e) {
            JOptionPane.showMessageDialog(null,
                    "Ошибка");
            session.getTransaction().rollback();
        }
        finally {
            HibernateUtil.closeSession();
        }
    }

    public void classTimeTableReport() {
        session = HibernateUtil.getInstance().getSession();
        try {
            session.beginTransaction();
            org.hibernate.Query query = session.getNamedQuery("Lesson.byClassDay");
            SchoolClass schoolClass = (SchoolClass) classComboBox.getSelectedItem();
            query.setInteger(0, schoolClass.getClassId());
            DayOfWeek dayOfWeek = (DayOfWeek) dayComboBox.getSelectedItem();
            query.setInteger(1, dayOfWeek.getDayId());
            lessonList = (List<Lesson>) query.list();
            session.getTransaction().commit();
        }
        catch (HibernateException e) {
            JOptionPane.showMessageDialog(null,
                    "Ошибка");
            session.getTransaction().rollback();
        }
        finally {
            HibernateUtil.closeSession();
        }
    }

}
